package com.yanfan.energy.service.impl;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.energy.domain.Workshop;
import com.yanfan.energy.domain.statist.DateStatistics;
import com.yanfan.energy.mapper.BuildingDao;
import com.yanfan.energy.mapper.WorkshopDao;
import com.yanfan.energy.service.WorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车间表(Workshop)表服务实现类
 *
 * @author makejava
 * @since 2022-09-26 13:50:17
 */
@Service("workshopService")
@Transactional(rollbackFor = Exception.class)
public class WorkshopServiceImpl implements WorkshopService {
    @Resource
    private WorkshopDao workshopDao;

    @Autowired
    private BuildingDao buildingDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Workshop queryById(Integer id) {
        return this.workshopDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param workshop    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Workshop> queryByPage(Workshop workshop, PageRequest pageRequest) {
        long total = this.workshopDao.count(workshop);
        return new PageImpl<>(this.workshopDao.queryAllByLimit(workshop, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param workshop 实例对象
     * @return 实例对象
     */
    @Override
    public Workshop insert(Workshop workshop) {
        this.workshopDao.insert(workshop);
        return workshop;
    }

    /**
     * 修改数据
     *
     * @param workshop 实例对象
     * @return 实例对象
     */
    @Override
    public Workshop update(Workshop workshop) {
        this.workshopDao.update(workshop);
        return this.queryById(workshop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.workshopDao.deleteById(id) > 0;
    }

    @Override
    public List<DateStatistics> findWorkshop() {
        // 不展示公共车间
        return this.workshopDao
                .findAllWorkShop()
                .stream()
                .filter(w -> !"公共".equals(w.getDate()))
                .collect(Collectors.toList());
    }

    @Override
    public Workshop findWorkshopByShopName(String shopName) {
        if ("EPO纯化".equals(shopName)) {
            return this.workshopDao.findWorkshopByshopName("%" + shopName + "%");
        }
        return this.workshopDao.findWorkshopByshopName("%" + shopName + "%");
    }

    @Override
    public AjaxResult shortWorkshop() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin()) {
            return AjaxResult.success(buildingDao.queryAll());
        }
        return AjaxResult.success(buildingDao.queryAllByDeptId(SecurityUtils.getDeptId().intValue()));
    }
}
